package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.adapter.NewDolistAdapter;
import com.hxkcloud.nurse.beans.DoOrderEditResultBean;
import com.hxkcloud.nurse.beans.DolistBean;
import com.hxkcloud.nurse.beans.DolistEditFuncBean;
import com.hxkcloud.nurse.beans.DolistItemBean;
import com.hxkcloud.nurse.beans.DolistTimeBean;
import com.hxkcloud.nurse.beans.MonitorKindBean;
import com.hxkcloud.nurse.beans.NoticeFunctionBean;
import com.hxkcloud.nurse.dborm.MonitorKindDao;
import com.hxkcloud.nurse.popup.DayIndexSelectPopWindow;
import com.hxkcloud.nurse.popup.NoticeTypeSelectPopWindow;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-07-11.
 * Description:新建用户执行单
 */
public class NewDolistActivity extends BaseActivity implements View.OnClickListener, NewDolistAdapter.NewDolistListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_save;

    private EditText et_name;
    private RecyclerView rv;

    private ArrayList<DolistItemBean> doItems;
    private NewDolistAdapter adapter;

    //两个按钮
    private PopupWindow popup_easynotice;
    private TextView tv_easynotice_title;
    private TextView tv_easynotice_cancel;
    private TextView tv_easynotice_func;
    //一个按钮
    private PopupWindow popup_easynotice1;
    private TextView tv_easynotice1_title;
    private TextView tv_easynotice1_func;
    //提醒类型选择
    private NoticeTypeSelectPopWindow typeWindow;
    //天数选择（1-50）
    private DayIndexSelectPopWindow dayWindow;

    private int funcType = 0;//列表监听操作内容，1删除整项提醒，2删除提醒下某个操作，3添加一个整提醒项，4保存另存为
    private int noticePosition = 0;//操作整项提醒的位置
    private int noticeGroup = 0;//操作提醒下某个操作的提醒位置
    //    private int noticeType = 0;//操作提醒下某个操作的类型，0monitor1content2article
    private int noticeChild = 0;//操作提醒下某个操作的操作位置

    private DolistBean existBean;//Intent接收，不为空的话为修改，点修改按钮返回

    private List<MonitorKindBean> monitors;//监测列表

    private int type = 0;//0执行单 1提醒单

    private String title = "";//标题

    private boolean isMy = false;//是不是当前登录护士创建的

    private String patientId = "";//有的话给这一个使用，否则去选择患者

    private boolean belongUser = false;//判断是否是修改用户的执行单，此时保存不需要判断保存、另存为，直接保存,保存成功后返回不需要预览

    private static List<MonitorKindBean> kinds;

    private int editType = 0;//0覆盖1另存为

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DOORDEREDIT_SUCCESS:
//                if (msg.obj != null) {
//                    toastMsg(msg.obj.toString());
//                }
//                Intent iPreview = new Intent(mContext, DolistPreviewActivity.class);
//                DolistBean doBean = new DolistBean();
//                doBean.setOrderName(title);
//                doBean.setOrderDetailList(doItems);
//                iPreview.putExtra("item", (Serializable) doBean);
//                startActivity(iPreview);

//                toastMsg(R.string.datacommitsuccess);

                if (editType == 0) {
                    if (type == 0) {
                        toastMsg("覆盖执行单成功");
                    } else if (type == 1) {
                        toastMsg("覆盖提醒单成功");
                    }
                } else if (editType == 1) {
                    if (type == 0) {
                        toastMsg("另存为执行单成功");
                    } else if (type == 1) {
                        toastMsg("另存为提醒单成功");
                    }
                }
                if (belongUser) {
                    finish();
                } else {
                    DoOrderEditResultBean result = (DoOrderEditResultBean) msg.obj;
                    Intent iPreview = new Intent(mContext, DolistPreviewActivity.class);
                    iPreview.putExtra("title", title);
                    iPreview.putExtra("id", result.getOrderId());
                    iPreview.putExtra("type", type);
                    iPreview.putExtra("patientId", patientId);
                    startActivity(iPreview);
                    finish();
                }

                break;
            case MessageCode.NETWORKCODE_DOORDEREDIT_FAILURE:
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datacommitfail);
                }
                break;
            case MessageCode.NETWORKCODE_MONITORKIND_SUCCESS:
                kinds = (List<MonitorKindBean>) msg.obj;
                if (kinds != null && kinds.size() > 0) {
//                    MonitorKindDao monitorKindDao = new MonitorKindDao(mContext);
//                    monitorKindDao.setKind(kinds, "1");
                    typeWindow.initDatas(kinds);
                }
                typeWindow.show(ll_title);

                break;
        }
    }

    @Override
    protected void onDestroy() {

        new Thread() {
            @Override
            public void run() {
                super.run();
                if (kinds != null && kinds.size() > 0) {
                    MonitorKindDao monitorKindDao = new MonitorKindDao(mContext);
                    monitorKindDao.setKind(kinds, "1");
                    kinds = null;
                }
            }
        }.start();

        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newdolist);

        type = this.getIntent().getIntExtra("type", 0);
        existBean = (DolistBean) this.getIntent().getSerializableExtra("item");
        isMy = this.getIntent().getBooleanExtra("isMy", false);
        patientId = this.getIntent().getStringExtra("patientId");
        belongUser = this.getIntent().getBooleanExtra("user", false);

//        ParcelableBean pbean = this.getIntent().getParcelableExtra("p");
//        if (pbean != null) {
//            System.out.println(pbean.getName());
//            System.out.println(pbean.getId());
//        }

        initView();
        initPopup();

        if (existBean != null) {
            for (DolistItemBean bean : existBean.getOrderDetailList()) {
                for (NoticeFunctionBean func : bean.getItemList()) {
                    if (func.getType().equals("2")) {
                        bean.setHasContents(true);
                    }
                }
            }
            et_name.setText(existBean.getOrderName());
            doItems = (ArrayList<DolistItemBean>) existBean.getOrderDetailList();
            adapter.setData(doItems);
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_save = (TextView) findViewById(R.id.tv_title_func);

        et_name = (EditText) findViewById(R.id.et_newdolist_name);
        rv = (RecyclerView) findViewById(R.id.rv_newdolist);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        if (type == 0) {
            tv_title.setText(getResources().getText(R.string.userdolist));
            et_name.setHint(R.string.clickinputdolistname);
        } else {
            tv_title.setText(getResources().getText(R.string.remindlist));
            et_name.setHint(R.string.clickinputremindlistname);
        }
        tv_save.setVisibility(View.VISIBLE);
        tv_save.setText(getResources().getText(R.string.save));
        iv_back.setOnClickListener(this);
        tv_save.setOnClickListener(this);

        doItems = new ArrayList<>();
        DolistItemBean bean = new DolistItemBean();//默认先添加一个，不可删除
        ArrayList<DolistTimeBean> times = new ArrayList<>();
        DolistTimeBean time = new DolistTimeBean();
        time.setTime("08:00");
        times.add(time);
        bean.setTimeIds(times);
//        bean.setArticleIds(new ArrayList<DolistArticleBean>());
//        bean.setCheckIds(new ArrayList<NoticeFunctionBean>());
        bean.setItemList(new ArrayList<NoticeFunctionBean>());
        bean.setDay("1");
        doItems.add(bean);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mLayoutManager);
        adapter = new NewDolistAdapter(mContext, doItems, type, screenWidth, this);
        rv.setAdapter(adapter);

        MonitorKindDao monitorKindDao = new MonitorKindDao(mContext);
        monitors = monitorKindDao.getKinds();
    }

    private void initPopup() {
        //easynotice
        popup_easynotice = new PopupWindow(this);
        View viewNotice = LayoutInflater.from(this).inflate(R.layout.popup_easynotice, null);
        popup_easynotice.setContentView(viewNotice);

        popup_easynotice.setWidth((int) (screenWidth * 0.7));
        popup_easynotice.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_easynotice.setFocusable(true);
        popup_easynotice.setTouchable(true);
        popup_easynotice.setOutsideTouchable(true);
        popup_easynotice.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_easynotice.setAnimationStyle(R.style.popupwindow_anim_alpha);

        tv_easynotice_title = (TextView) viewNotice.findViewById(R.id.tv_easynotice_title);
        tv_easynotice_cancel = (TextView) viewNotice.findViewById(R.id.tv_easynotice_cancel);
        tv_easynotice_func = (TextView) viewNotice.findViewById(R.id.tv_easynotice_func);

        tv_easynotice_cancel.setOnClickListener(this);
        tv_easynotice_func.setOnClickListener(this);

        popup_easynotice.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(NewDolistActivity.this);
                funcType = 0;
            }
        });

        //easynotice1
        popup_easynotice1 = new PopupWindow(this);
        View viewNotice1 = LayoutInflater.from(this).inflate(R.layout.popup_easynotice1, null);
        popup_easynotice1.setContentView(viewNotice1);

        popup_easynotice1.setWidth((int) (screenWidth * 0.7));
        popup_easynotice1.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_easynotice1.setFocusable(true);
        popup_easynotice1.setTouchable(true);
        popup_easynotice1.setOutsideTouchable(true);
        popup_easynotice1.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_easynotice1.setAnimationStyle(R.style.popupwindow_anim_alpha);

        tv_easynotice1_title = (TextView) viewNotice1.findViewById(R.id.tv_easynotice1_title);
        tv_easynotice1_func = (TextView) viewNotice1.findViewById(R.id.tv_easynotice1_func);

        tv_easynotice1_func.setOnClickListener(this);

        popup_easynotice1.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(NewDolistActivity.this);
                funcType = 0;
            }
        });

        //dayindex
        dayWindow = new DayIndexSelectPopWindow(NewDolistActivity.this);
        dayWindow.setDayListener(new DayIndexSelectPopWindow.onDaySelectListener() {
            @Override
            public void selectDay(String day) {
//                int last = Integer.valueOf(doItems.get(doItems.size() - 1).getDay().replace("第", "").replace("天", ""));
//                int newd = Integer.valueOf(day.replace("第", "").replace("天", ""));
                String dayStr = day.substring(1, day.length() - 1);
                doItems.get(noticePosition).setDay(dayStr);
                adapter.setData(doItems);
            }
        });

        //提醒类型选择
        typeWindow = new NoticeTypeSelectPopWindow(NewDolistActivity.this);
        typeWindow.setNoticeTypeListener(new NoticeTypeSelectPopWindow.NoticeTypeListener() {
            @Override
            public void setNoticeType(String t) {
                int type = Integer.valueOf(t);
                typeWindow.dismiss();
//                DolistItemBean bean = doItems.get(noticePosition);

                if (type < monitors.size()) {
                    boolean exist = false;
                    for (int i = 0; i < doItems.get(noticePosition).getItemList().size(); i++) {
                        if (doItems.get(noticePosition).getItemList().get(i).getItemId().equals(monitors.get(type).getKindId())) {
                            if (doItems.get(noticePosition).getItemList().get(i).getType().equals("0")) {
                                exist = true;
                                break;
                            }
                        }
                    }

                    if (exist) {
                        toastMsg(R.string.hasmonitorremind);
                    } else {
                        NoticeFunctionBean function = new NoticeFunctionBean();
                        function.setContent(monitors.get(type).getName());
                        function.setItemId(monitors.get(type).getKindId());
                        function.setType(YjhApplication.doorder_type_check);

                        doItems.get(noticePosition).getItemList().add(function);
                        adapter.setData(doItems);
                    }
                } else if (type == monitors.size()) {//content
                    if (doItems.get(noticePosition).isHasContents()) {
                        toastMsg(R.string.hasremindword);
                    } else {
                        doItems.get(noticePosition).setHasContents(true);
                        NoticeFunctionBean function = new NoticeFunctionBean();
                        function.setContent("");
                        function.setItemId("");
                        function.setType(YjhApplication.doorder_type_content);

                        doItems.get(noticePosition).getItemList().add(function);
                        adapter.setData(doItems);
                    }
                } else {//article
                    //单选文章
//                    Intent iCourse = new Intent(mContext, NewsActivity.class);
//                    iCourse.putExtra("type", YjhApplication.articlekind_course);
//                    iCourse.putExtra("choose", true);
//                    startActivityForResult(iCourse, 123);

                    Intent iCourse = new Intent(mContext, AddArticalActivity.class);
                    startActivityForResult(iCourse, 123);
                }

//                if (type == 0) {
//                    NoticeFunctionBean function = new NoticeFunctionBean();
//                    function.setName("测量血压");
//                    function.setType(0);
//                    doItems.get(noticePosition).getFunctions().add(function);
//                    adapter.setData(doItems);
//                } else if (type == 1) {
//                    NoticeFunctionBean function = new NoticeFunctionBean();
//                    function.setName("测量血糖");
//                    function.setType(1);
//                    doItems.get(noticePosition).getFunctions().add(function);
//                    adapter.setData(doItems);
//                } else if (type == 2) {
//                    NoticeFunctionBean function = new NoticeFunctionBean();
//                    function.setName("");
//                    function.setType(2);
//                    doItems.get(noticePosition).getFunctions().add(function);
//                    adapter.setData(doItems);
//                } else if (type == 3) {
//
//                }
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.tv_title_func:
                title = et_name.getText().toString().trim();
                if (StringUtil.isEmpty(title)) {
                    Toast.makeText(mContext, R.string.pleaseinputtitle, Toast.LENGTH_LONG).show();
                    return;
                }
                for (DolistItemBean item : doItems) {
                    if (item.getItemList() == null || item.getItemList().size() == 0) {
                        Toast.makeText(mContext, R.string.pleaseinputwholecontent, Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (item.getTimeIds() == null || item.getTimeIds().size() == 0) {
                        Toast.makeText(mContext, R.string.pleaseinputwholecontent, Toast.LENGTH_LONG).show();
                        return;
                    }
                }

                if (existBean != null) {
                    if (belongUser) {
                        loading();
                        System.out.println("response=new Gson().toJson(postParameter)===" + doItems);
                        List<DolistEditFuncBean> ChildrenList = new ArrayList<>();
                        for (DolistItemBean item : doItems) {
                            DolistEditFuncBean func = new DolistEditFuncBean();
                            List<DolistEditFuncBean.funcBean> fs = new ArrayList<>();
                            for (NoticeFunctionBean f : item.getItemList()) {
                                DolistEditFuncBean.funcBean fb = new DolistEditFuncBean.funcBean();
                                fb.setContent(f.getContent());
                                fb.setRemindId(f.getItemId());
                                fb.setType(f.getType());
                                fs.add(fb);
                            }
                            if (type == 0) {
                                func.setDay("");
                            } else {
                                func.setDay(item.getDay().replace("第", "").replace("天", ""));
                            }
                            List<String> times = new ArrayList<>();
                            for (DolistTimeBean t : item.getTimeIds()) {
                                times.add(t.getTime());
                            }
                            func.setList(fs);
                            func.setTime(times);
                            ChildrenList.add(func);
                        }

                        YiJiaHuNetWorkUtils.doOrderEdit(existBean.getOrderId(), type, title, ChildrenList, handler);
                    } else {
                        tv_easynotice_title.setText(getResources().getString(R.string.choosesavekind));
                        tv_easynotice_func.setText(getResources().getString(R.string.cover));
                        tv_easynotice_cancel.setText(getResources().getString(R.string.saveas));

                        funcType = 4;
//                        noticePosition = position;
                        showNoticePopup(2);
                    }


                } else {
                    loading();
//                        List<DoOrderEditBean> orderDetailList = new ArrayList<>();
//                        for (DolistItemBean item : doItems) {
//                            DoOrderEditBean bean = new DoOrderEditBean();
////                            if (item.isHasContents()) {
////                                bean.setContents(item.getContents());
////                            }
//                            List<Integer> articleIds = new ArrayList<>();
////                            for (DolistArticleBean article : item.getArticleIds()) {
////                                articleIds.add(Integer.valueOf(article.getArticleId()));
////                            }
//                            List<Integer> checkIds = new ArrayList<>();
////                            for (NoticeFunctionBean func : item.getCheckIds()) {
////                                checkIds.add(Integer.valueOf(func.getMonitorId()));
////                            }
//                            for (NoticeFunctionBean f : item.getItemList()) {
//                                if (f.getType().equals(YjhApplication.doorder_type_content)) {
//                                    bean.setContents(f.getContent());
//                                }
//                                if (f.getType().equals(YjhApplication.doorder_type_article)) {
//                                    articleIds.add(Integer.valueOf(f.getItemId()));
//                                }
//                                if (f.getType().equals(YjhApplication.doorder_type_check)) {
//                                    checkIds.add(Integer.valueOf(f.getItemId()));
//                                }
//                            }
//                            List<String> timeInfos = new ArrayList<>();
//                            for (DolistTimeBean time : item.getTimeIds()) {
//                                timeInfos.add(time.getTime());
//                            }
//                            bean.setArticleIds(articleIds);
//                            bean.setCheckIds(checkIds);
//                            bean.setTimeInfos(timeInfos);
//                            bean.setDay(item.getDay().replace("第", "").replace("天", ""));
//                            orderDetailList.add(bean);
//                        }

                    List<DolistEditFuncBean> ChildrenList = new ArrayList<>();
                    for (DolistItemBean item : doItems) {
                        DolistEditFuncBean func = new DolistEditFuncBean();
                        List<DolistEditFuncBean.funcBean> fs = new ArrayList<>();
                        for (NoticeFunctionBean f : item.getItemList()) {
                            DolistEditFuncBean.funcBean fb = new DolistEditFuncBean.funcBean();
                            fb.setContent(f.getContent());
                            fb.setRemindId(f.getItemId());
                            fb.setType(f.getType());
                            fs.add(fb);
                        }
                        if (type == 0) {
                            func.setDay("");
                        } else {
                            func.setDay(item.getDay().replace("第", "").replace("天", ""));
                        }
                        List<String> times = new ArrayList<>();
                        for (DolistTimeBean t : item.getTimeIds()) {
                            times.add(t.getTime());
                        }
                        func.setList(fs);
                        func.setTime(times);
                        ChildrenList.add(func);
                    }

                    YiJiaHuNetWorkUtils.doOrderEdit("", type, title, ChildrenList, handler);

//                        Intent iPreview = new Intent(mContext, DolistPreviewActivity.class);
//                        DolistBean doBean = new DolistBean();
//                        doBean.setOrderName(title);
//                        doBean.setOrderDetailList(doItems);
//                        iPreview.putExtra("item", (Serializable) doBean);
//                        startActivity(iPreview);
                }
                break;
            case R.id.tv_easynotice_cancel:
                if (funcType == 4) {//另存一个
                    if (title.equals(existBean.getOrderName())) {
                        title = title + "-1";
                    }
                    loading();
                    List<DolistEditFuncBean> ChildrenList = new ArrayList<>();
                    for (DolistItemBean item : doItems) {
                        DolistEditFuncBean func = new DolistEditFuncBean();
                        List<DolistEditFuncBean.funcBean> fs = new ArrayList<>();
                        for (NoticeFunctionBean f : item.getItemList()) {
                            DolistEditFuncBean.funcBean fb = new DolistEditFuncBean.funcBean();
                            fb.setContent(f.getContent());
                            fb.setRemindId(f.getItemId());
                            fb.setType(f.getType());
                            fs.add(fb);
                        }
                        if (type == 0) {
                            func.setDay("");
                        } else {
                            func.setDay(item.getDay().replace("第", "").replace("天", ""));
                        }
                        List<String> times = new ArrayList<>();
                        for (DolistTimeBean t : item.getTimeIds()) {
                            times.add(t.getTime());
                        }
                        func.setList(fs);
                        func.setTime(times);
                        ChildrenList.add(func);
                    }

                    editType = 1;
                    YiJiaHuNetWorkUtils.doOrderEdit("", type, title, ChildrenList, handler);
                    popup_easynotice.dismiss();
                }

                popup_easynotice.dismiss();
                break;
            case R.id.tv_easynotice_func:
                if (funcType == 1) {//删除整项提醒
                    doItems.remove(noticePosition);
                    adapter.setData(doItems);
                    popup_easynotice.dismiss();
                } else if (funcType == 3) {//添加一个整提醒项
                    DolistItemBean newItem = new DolistItemBean();
//                    newItem.setCheckIds(new ArrayList<NoticeFunctionBean>());
//                    newItem.setArticleIds(new ArrayList<DolistArticleBean>());
                    newItem.setItemList(new ArrayList<NoticeFunctionBean>());
                    newItem.setTimeIds(new ArrayList<DolistTimeBean>());
                    newItem.setDay("1");
                    doItems.add(newItem);
                    adapter.setData(doItems);
                    popup_easynotice.dismiss();
                } else if (funcType == 2) {//删除提醒下某个操作
//                    if (noticeType == 0) {//0monitor1content2article
//                        doItems.get(noticeGroup).getTimeIds().remove(noticeChild);
//                    } else if (noticeType == 1) {
//                        doItems.get(noticeGroup).setHasContents(false);
//                        doItems.get(noticeGroup).setContents("");
//                    } else {
//                        doItems.get(noticeGroup).getArticleIds().remove(noticeChild);
//                    }
//                    if (doItems.get(noticeGroup).getItemList().get(noticeChild).getType().equals(YjhApplication.doorder_type_content)) {
//                        doItems.get(noticeGroup).setHasContents(false);
//                    }
                    if (doItems.get(noticeGroup).getItemList().get(noticeChild).getType().equals("2")) {
                        doItems.get(noticeGroup).setHasContents(false);
                    }
                    doItems.get(noticeGroup).getItemList().remove(noticeChild);
                    adapter.setData(doItems);
                    popup_easynotice.dismiss();
                } else if (funcType == 4) {
                    if (isMy) {//保存我的
                        loading();
                        List<DolistEditFuncBean> ChildrenList = new ArrayList<>();
                        for (DolistItemBean item : doItems) {
                            DolistEditFuncBean func = new DolistEditFuncBean();
                            List<DolistEditFuncBean.funcBean> fs = new ArrayList<>();
                            for (NoticeFunctionBean f : item.getItemList()) {
                                DolistEditFuncBean.funcBean fb = new DolistEditFuncBean.funcBean();
                                fb.setContent(f.getContent());
                                fb.setRemindId(f.getItemId());
                                fb.setType(f.getType());
                                fs.add(fb);
                            }
                            if (type == 0) {
                                func.setDay("");
                            } else {
                                func.setDay(item.getDay().replace("第", "").replace("天", ""));
                            }
                            List<String> times = new ArrayList<>();
                            for (DolistTimeBean t : item.getTimeIds()) {
                                times.add(t.getTime());
                            }
                            func.setList(fs);
                            func.setTime(times);
                            ChildrenList.add(func);
                        }

                        editType = 0;
                        YiJiaHuNetWorkUtils.doOrderEdit(existBean.getOrderId(), type, title, ChildrenList, handler);
                    } else {
                        toastMsg(R.string.onlysaveas);
                    }
                }
                break;
            case R.id.tv_easynotice1_func:

                break;
        }
    }

    public void showNoticePopup(int type) {
        if (type == 1) {
            popup_easynotice1.showAtLocation(ll_title, Gravity.CENTER, 0, 0);
            BrightnessUtils.lightOff(this);
            closeInputMethodManager();
        } else if (type == 2) {
            popup_easynotice.showAtLocation(ll_title, Gravity.CENTER, 0, 0);
            BrightnessUtils.lightOff(this);
            closeInputMethodManager();
        }
    }

    //点击左侧标志按钮，0无操作，最后一个添加，中间的删除
    @Override
    public void onSignClick(int position) {
        if (position == 0) {//nothing

        } else if (position < doItems.size()) {//删除整项提醒
//            DolistItemBean bean = doItems.get(position);

            tv_easynotice_title.setText(getResources().getString(R.string.isdelthisnotice));
            tv_easynotice_cancel.setText(getResources().getString(R.string.cancel));
            tv_easynotice_func.setText(getResources().getString(R.string.delete));

            funcType = 1;
            noticePosition = position;
            showNoticePopup(2);

        } else {//添加整项提醒
            tv_easynotice_title.setText(getResources().getString(R.string.isaddnewnoticeitem));
            tv_easynotice_cancel.setText(getResources().getString(R.string.cancel));
            tv_easynotice_func.setText(getResources().getString(R.string.sure));

            funcType = 3;
            showNoticePopup(2);
        }
    }

    //点击任一项的添加提醒,选择血压提醒，血糖提醒，文字提醒，文章提醒
    @Override
    public void onAddnotice(int position) {
        closeInputMethodManager();
        noticePosition = position;

        if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
            loading();
            YiJiaHuNetWorkUtils.getMonitorKind(loginInfo.getDepartmentId(), handler);
        }

    }

    @Override
    public void addDayIndex(int position) {
        if (position != 0) {
            noticePosition = position;
            dayWindow.show(ll_title);
            closeInputMethodManager();
        }
    }

    //点击删除提醒项的操作
    @Override
    public void onDeleteFunction(int group, /*int type,*/ int child) {
//        DolistItemBean bean = doItems.get(group);
//        NoticeFunctionBean function = bean.getFunctions().get(child);
        tv_easynotice_title.setText(getResources().getString(R.string.isdelthisnotice));
        tv_easynotice_cancel.setText(getResources().getString(R.string.cancel));
        tv_easynotice_func.setText(getResources().getString(R.string.delete));

        funcType = 2;
        noticeGroup = group;
//        noticeType = type;
        noticeChild = child;
        showNoticePopup(2);
    }

    //添加时间点
    @Override
    public void onAddTime(int position) {
        DolistItemBean bean = doItems.get(position);
        Intent iTime = new Intent(this, NoticeTimeSelectActivity.class);
        iTime.putExtra("position", position);

        ArrayList<String> times = new ArrayList<>();
        for (DolistTimeBean t : bean.getTimeIds()) {
            times.add(t.getTime());
        }
        iTime.putExtra("type", type + "");
        iTime.putStringArrayListExtra("times", times);
        startActivityForResult(iTime, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0) {
                int position = data.getIntExtra("position", 0);
                ArrayList<String> times = data.getStringArrayListExtra("times");

                ArrayList<DolistTimeBean> timebs = new ArrayList<>();
                for (String t : times) {
                    DolistTimeBean tb = new DolistTimeBean();
                    tb.setTime(t);
                    timebs.add(tb);
                }

                doItems.get(position).setTimeIds(timebs);
                adapter.setData(doItems);
            } else if (requestCode == 123) {
                //单选
//                String aId = data.getStringExtra("id");
//                String aName = data.getStringExtra("name");
//
//                boolean exist = false;
//                for (int i = 0; i < doItems.get(noticePosition).getItemList().size(); i++) {
//                    if (doItems.get(noticePosition).getItemList().get(i).getItemId().equals(aId)) {
//                        if (doItems.get(noticePosition).getItemList().get(i).getType().equals(YjhApplication.doorder_type_article)) {
//                            exist = true;
//                            break;
//                        }
//                    }
//                }
//
//                if (exist) {
//                    toastMsg(R.string.hascoursearticle);
//                } else {
//                    NoticeFunctionBean function = new NoticeFunctionBean();
//                    function.setContent(aName);
//                    function.setItemId(aId);
//                    function.setType(YjhApplication.doorder_type_article);
//
//                    doItems.get(noticePosition).getItemList().add(function);
//                    adapter.setData(doItems);

                //多选
                String[] ids = data.getStringArrayExtra("ids");
                String[] names = data.getStringArrayExtra("names");

                for (int j = 0; j < ids.length; j++) {
                    boolean exist = false;
                    for (int i = 0; i < doItems.get(noticePosition).getItemList().size(); i++) {
                        if (doItems.get(noticePosition).getItemList().get(i).getItemId().equals(ids[j])) {
                            if (doItems.get(noticePosition).getItemList().get(i).getType().equals(YjhApplication.doorder_type_article)) {
                                exist = true;
                            }
                        }
                    }
                    if (!exist) {
                        int articleCount = 0;
                        for (NoticeFunctionBean func : doItems.get(noticePosition).getItemList()) {
                            if (func.getType().equals(YjhApplication.doorder_type_article)) {
                                articleCount++;
                            }
                        }
                        if (articleCount >= 10) {
                            toastMsg(R.string.most10articles);
                            break;
                        } else {
                            NoticeFunctionBean function = new NoticeFunctionBean();
                            function.setContent(names[j]);
                            function.setItemId(ids[j]);
                            function.setType(YjhApplication.doorder_type_article);

                            doItems.get(noticePosition).getItemList().add(function);
                        }
                    }
                }
                adapter.setData(doItems);
//                }
            }
        }
    }
}
